<?php

require FCPATH . '/application/controllers/backend/backend.php';

class User extends Backend {

	public function __construct() {
		parent::__construct();

		$this->load->model('user_model', 'model');
	}

	public function index() {
		$this->load_js(array('jquery-ui-1.8.20.custom.min', 'backend_user'));
		$this->load_css('custom-theme/jquery-ui-1.8.20.custom');
		$this->load->model('permission_group_model');

		$data = array(
			'l_position' => array(
				array('name' => '帐号管理')
			),
		    'permission_groups' => $this->permission_group_model->records()
		);

		$this->rend('user_index', $data);
	}

	public function records() {
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page == 0 ? 1 : $page;
		$offset = $page_size * ($page - 1);

		$id = intval($this->input->get('id'));
		$username = trim($this->input->get('username'));
		$permission_group = trim($this->input->get('permission_group'));
		$sex = intval($this->input->get('sex'));
		$is_freezed = intval($this->input->get('is_freezed'));
		$start = trim($this->input->get('start'));
		$end = trim($this->input->get('end'));

		$conditions = array(
			array('where','id >',1)
		);

		if(!empty($id)){
			$conditions[] = array('where', 'id', $id);
		}

		if (!empty($username)) {
			$conditions[] = array('like', 'username', $username);
		}

		if (!empty($permission_group)) {
			$conditions[] = array('where', 'permission_group', $permission_group);
		}

		if (!empty($sex)) {
			$conditions[] = array('where', 'sex', $sex);
		}

		if (!empty($is_freezed)) {
			$conditions[] = array('where', 'is_freezed', $is_freezed);
		}

		if (!empty($start)) {
			$conditions[] = array('where', 'created_at >=', $start);
		}

		if (!empty($end)) {
			$conditions[] = array('where', 'created_at <=', $end);
		}

		$order_fields = array('username','permission_group', 'sex', 'email', 'is_freezed', 'created_at');
		$order_field = intval($this->input->get('order_field'));
		$order_type = intval($this->input->get('order_type'));
		$orders = array();
		if ($order_field >= 0 && $order_field < count($order_fields)) {
			$orders = array(
				array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
			);
		}

		$this->json_back('XT001', '', array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
		));
	}

	public function insert() {
		$data = array(
			'username' => trim($this->input->post('username')),
			'password' => trim($this->input->post('password')),
			'repeat_password' => trim($this->input->post('repeat_password')),
			'sex' => intval($this->input->post('sex')),
			'email' => trim($this->input->post('email')),
			'permission_group' => intval($this->input->post('permission_group'))
		);

		if (empty($data['username'])) {
			$this->json_back('XT002', '请输入帐号名称');
		}

		if (empty($data['password'])) {
			$this->json_back('XT002', '请输入帐号密码');
		}

		if (empty($data['repeat_password'])) {
			$this->json_back('XT002', '请输入重复密码');
		}

		if ($data['password'] != $data['repeat_password']) {
			$this->json_back('XT002', '密码不一致');
		}

		unset($data['repeat_password']);

		$data['password'] = md5($data['password']);

		if (($id = $this->model->insert($data)) > 0) {
			$this->json_back('XT001', '添加成功', array('id' => $id));
		} else {
			$this->json_back('XT002', '添加失败，sql或数据库错误');
		}
	}

	public function update() {
		$id = intval($this->input->post('id'));

		if (!$this->model->find($id)) {
			$this->json_back('XT002', 'ID错误或该记录已被删除。');
		}

		$data = array(
			'username' => trim($this->input->post('username')),
			'sex' => intval($this->input->post('sex')),
			'email' => trim($this->input->post('email')),
			'permission_group' => intval($this->input->post('permission_group'))
		);

		if ($this->model->update($id, $data)) {
			$this->json_back('XT001', '更新成功', array('id' => $id));
		} else {
			$this->json_back('XT002', '更新失败，sql或数据库错误');
		}
	}

	public function freeze() {
		$id = intval($this->uri->segment(4));

		if($id == 1){
			$this->json_back('XT002', '初始化用户不能被冻结。');
		}

		if (!$this->model->find($id)) {
			$this->json_back('XT002', 'ID错误或该记录已被删除。');
		}

		$data = array('is_freezed' => 1);

		if ($this->model->update($id, $data)) {
			$this->json_back('XT001', '冻结成功', array('id' => $id));
		} else {
			$this->json_back('XT002', '冻结失败，sql或数据库错误');
		}
	}

	public function unfreeze() {
		$id = intval($this->uri->segment(4));

		if (!$this->model->find($id)) {
			$this->json_back('XT002', 'ID错误或该记录已被删除。');
		}

		$data = array('is_freezed' => 2);

		if ($this->model->update($id, $data)) {
			$this->json_back('XT001', '解冻成功', array('id' => $id));
		} else {
			$this->json_back('XT002', '解冻失败，sql或数据库错误');
		}
	}

	//用于用户修改自己的密码
	public function change_password(){
		$id = intval($this->session->userdata('g_user_id'));

		$password = $this->input->post('password');
		$repeat_password = $this->input->post('repeat_password');

		if($password != $repeat_password){
			$this->json_back('XT002', '密码不一致');
		}

		if($this->model->update($id,array('password'=>md5($password)))){
			$this->json_back('XT001', '修改密码成功', array('id' => $id));
		} else {
			$this->json_back('XT002', '修改密码失败，sql或数据库错误');
		}
	}

	//用于超级管理员，重设任何人的密码
	public function reset_password(){

	}
}
